<?php

class View {

	function __construct($viewName, $vars = null) {
		$path = "/views/$viewName.php";
		$view_sys = SYSTEM_PATH . $path;
		$view_app = APP_PATH . $path;
		if( is_array($vars) ) {
			// replace BufferedViews with their contained buffers
			foreach( $vars as $key=>$var ) if( $var instanceof BufferedView ) $vars[$key] = $var->getBuffer();
			// put the variables in the local symbol table for use by the view itself
			extract( $vars );
		}
		if( file_exists($view_app) ) include( $view_app );
		elseif( file_exists($view_sys) ) include( $view_sys );
		else throw new Exception('Could not find view');
	}
	
	static function show( $var = null ) {
		if( $var !== null ) echo $var;
	}
	
	static function showIndented( $var = null, $indentation = 0, $indentFirstLine = false ) {
		if( $var !== null ) {
			if( indentFirstLine === true ) echo str_repeat("\t", $indentation);
			echo str_replace(array("\r", "\r\n", "\n"),	"\n" . str_repeat("\t", $indentation), $var );
		}
	}
	
}